package view.parameterpanels;

import java.awt.Component;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.IManufacturingMethodWithPartProcessesAndTools;
import net.miginfocom.swing.MigLayout;
import view.AlphaTextField;
import view.NumericalTextField;
import view.CenterTabbedPane.EBenchmarkTab;

/**
 * To draw a parameter panel for input parameters of type tool parameters.
 * On this panel the user can add and remove tools besides assigning parameter
 * information to the tools.
 * @author Oliver Eckstein & Sebastian Perlwitz
 *
 */
public class ToolParameterPanel extends StandardParameterInputPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * The text fields that allow the user to change a tool's name
	 */
	private List<AlphaTextField> toolNameTextFields;
	
	/**
	 * A button to add a new tool.
	 */
	private JButton addTool;
	
	/**
	 * A button to remove the last tool.
	 */
	private JButton removeTool;

	/**
	 * To create a new {@code ToolParameterPanel}.
	 * @param logoPath a path to an optional logo. If passed {@code null} a
	 * standard logo is used.
	 * @param tab the type of input parameters that is displayed on the panel.
	 * @param panelTitle the panel's title.
	 * @param manuMethod the model to be referenced.
	 */
	public ToolParameterPanel(
			String logoPath,
			EBenchmarkTab tab,
			String panelTitle,
			IManufacturingMethodWithPartProcessesAndTools manuMethod) {
		super(logoPath, tab, panelTitle, manuMethod);
		toolNameTextFields = new ArrayList<AlphaTextField>();
	}

	@Override
	public void update() {
		super.update();
		
		if(getErrorStatus()) {
			// To disable the buttons.
			addTool.setEnabled(false);
			removeTool.setEnabled(false);
		} else {
			// To enable the buttons after the input was corrected.
			if(!addTool.isEnabled()) {
				addTool.setEnabled(true);
				removeTool.setEnabled(true);
			}
		}
		
		// Deactivate the tool name fields.
		if(getErrorStatus()) {
			// To disable all text fields because of wrong input
			for(AlphaTextField txtField : toolNameTextFields) {
				if(!txtField.getErrorStatus()) {
					txtField.setEnabled(false);
				}
			}
		} else {
			// To enable all disabled text fields when wrong input was corrected
			for(AlphaTextField txtField : toolNameTextFields) {
				if(!txtField.isEnabled()) {
					txtField.setEnabled(true);
				}
			}
		}
	}
	
	@Override
	protected void drawSettings() {
		settings.add(new JLabel("Anzahl Werkzeuge festlegen: "));		
		addTool = new JButton("+");
		removeTool = new JButton("-");
		
		settings.add(addTool);
		settings.add(removeTool);
	}
	
	/**
	 * To add a tool parameter set to this panel.
	 @param paramInfo the parameters' information:<br>
     * paramInfo[0] = parameter's name<br>
     * paramInfo[1] = parameter's key for identification<br>
     * paramInfo[2] = parameter's abbreviation<br>
     * paramInfo[3] = parameter's unit<br>
     * paramInfo[4] = parameter's initial value
     * @param borderTitle the parameters' description.
     * @param toolKey the identifying key of the tool whose parameter
     * panel gets created.
     * @return the newly created text fields.
     */
	public List<JTextField> addParameterSet(
			List<String[]> paramInfo,
			String borderTitle,
			String toolKey) {		
		JPanel parameterPanel = new JPanel(new MigLayout("wrap 4"));
		List<JTextField> newTextFields = new ArrayList<JTextField>();
		
    	parameterPanel.setBorder(BorderFactory
                .createTitledBorder(borderTitle));
    	
    	parameterPanel.add(new JLabel("Beschreibung"));
    	parameterPanel.add(new JLabel(""));
    	parameterPanel.add(new JLabel(""));
    	
    	AlphaTextField alphaTF = new AlphaTextField(toolKey);
    	alphaTF.setText(borderTitle);    	
    	alphaTF.setCaretPosition(0);
    	parameterPanel.add(alphaTF);
    	toolNameTextFields.add(alphaTF);
    	newTextFields.add(alphaTF);
    	
    	
    	newTextFields.addAll(
        		addStandardParameterPanels(
        				parameterPanel, paramInfo, borderTitle));                
        return newTextFields;
	}
	
	/**
	 * To remove a parameter set from this panel.
	 * @return the removed text fields.
	 */
	public List<JTextField> removeParameterPanel() {
		
		int panelsCount = parameterPanels.size() - 1;
		List<JTextField> removedTextFields = 
				new ArrayList<JTextField>();
		List<NumericalTextField> removedNumericalTextFields = 
				new ArrayList<NumericalTextField>();
		List<AlphaTextField> removedAlphaTextFields = 
				new ArrayList<AlphaTextField>();
		
		if(panelsCount > 0) {			
			JPanel panelToRemove = parameterPanels.get(panelsCount);
			paramPanel.remove(panelToRemove);
			for(Component component : panelToRemove.getComponents()) {
				if(component instanceof NumericalTextField) {
					removedNumericalTextFields.add((NumericalTextField) component);
					removedTextFields.add((JTextField) component);
				} else if (component instanceof AlphaTextField) {
					removedAlphaTextFields.add((AlphaTextField) component);
					removedTextFields.add((JTextField) component);
				}
			}
			textFields.removeAll(removedNumericalTextFields);
			toolNameTextFields.removeAll(removedAlphaTextFields);
			parameterPanels.remove(panelsCount);
			this.revalidate();
		}
		return removedTextFields;
	}
	
	/**
	 * To get the text fields that change the name of the tool.
	 * @return the text fields.
	 */
	public List<AlphaTextField> getToolNameTextFields() {
		return toolNameTextFields;
	}
	
	/**
	 * To get the button that adds a new tool.
	 * @return the button
	 */
	public JButton getAddToolButton() {
		return addTool;
	}
	
	/**
	 * To get the button that removes a new tool.
	 * @return the button
	 */
	public JButton getRemoveToolButton() {
		return removeTool;
	}
}
